/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.hochschild.sgv.struts.action;

import com.hochschild.sgv.domain.Division;
import com.hochschild.sgv.domain.SubDivision;
import com.hochschild.sgv.service.FiltrosManager;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author LENOVO
 */
public class AjaxAction extends DispatchAction{
    @Autowired
    private FiltrosManager filtrosManager;

    public ActionForward cargarDivisiones(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String idSociedad = request.getParameter("idSociedad");
        List<Division> lstDivision = filtrosManager.getDivisiones(idSociedad);
        net.sf.json.JSONArray listaJsonArray = new net.sf.json.JSONArray();

        for(Division division : lstDivision){
            net.sf.json.JSONObject laborJsonObject = new net.sf.json.JSONObject();
            laborJsonObject.put("id", division.getIdDivision());
            laborJsonObject.put("descripcion", division.getDescripcion());
            listaJsonArray.add(laborJsonObject);
        }

	net.sf.json.JSONObject laborJsonResultado = new net.sf.json.JSONObject();
        laborJsonResultado.put("resultado", listaJsonArray);
        response.setContentType("application/json");
	response.getWriter().println(laborJsonResultado.toString());
        
        return null;
    }

        public ActionForward cargarSubDivisiones(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String idDivision = request.getParameter("idDivision");
        List<SubDivision> lstSubDivision = filtrosManager.getSubDivisiones(idDivision);
        net.sf.json.JSONArray listaJsonArray = new net.sf.json.JSONArray();

        for(SubDivision subDivision : lstSubDivision){
            net.sf.json.JSONObject laborJsonObject = new net.sf.json.JSONObject();
            laborJsonObject.put("id", subDivision.getIdSubDivision());
            laborJsonObject.put("descripcion", subDivision.getDescripcion());
            listaJsonArray.add(laborJsonObject);
        }

	net.sf.json.JSONObject laborJsonResultado = new net.sf.json.JSONObject();
        laborJsonResultado.put("resultado", listaJsonArray);
        response.setContentType("application/json");
	response.getWriter().println(laborJsonResultado.toString());

        return null;
    }
        //13-12
        public ActionForward cargarSubDivisionesVacacionesPlanificadas(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
            System.out.println("cargarSubDivisionesVacacionesPlanificadas");
         String idDivision = request.getParameter("idDivision");
         String esadmin = request.getParameter("esadmin");
         String subDivi = request.getParameter("subDivision");
            System.out.println("esadmin: " + esadmin + " subDivision: " + subDivi);
            
        List<SubDivision> lstSubDivision = filtrosManager.getSubDivisiones(idDivision);
        net.sf.json.JSONArray listaJsonArray = new net.sf.json.JSONArray();

            if (esadmin.toUpperCase().equals("X")) {
                for(SubDivision subDivision : lstSubDivision){
                    net.sf.json.JSONObject laborJsonObject = new net.sf.json.JSONObject();
                    laborJsonObject.put("id", subDivision.getIdSubDivision());
                    laborJsonObject.put("descripcion", subDivision.getDescripcion());
                    listaJsonArray.add(laborJsonObject);
                }
            } else {
                for(SubDivision subDivision : lstSubDivision){
                    net.sf.json.JSONObject laborJsonObject = new net.sf.json.JSONObject();
                    laborJsonObject.put("id", subDivision.getIdSubDivision());
                    laborJsonObject.put("descripcion", subDivision.getDescripcion());
                    if (subDivision.getIdSubDivision().equals(subDivi)) {
                        listaJsonArray.add(laborJsonObject);
                        break;
                    }    
                }
            }
        

	net.sf.json.JSONObject laborJsonResultado = new net.sf.json.JSONObject();
        laborJsonResultado.put("resultado", listaJsonArray);
        response.setContentType("application/json");
	response.getWriter().println(laborJsonResultado.toString());

        return null;
    }         
}